import Vue from 'vue';
import Router from 'vue-router';

const Login = () => import('../views/common/Login.vue');
const Home = () => import('../views/common/Home.vue');
const Users = () => import('../views/minibk/users/Users.vue');
const Bookmarks = () => import('../views/minibk/bookmarks/Bookmarks.vue');

Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: '/login',
      component: Login,
      name: '',
      hidden: true
    },
    {
      path: '/',
      component: Home,
      name: '首页',
      iconCls: 'el-icon-message', // 图标样式class
      children: [
        {
          path: 'users',
          name: '用户管理',
          component: Users
        },
        {
          path: 'bookmarks',
          name: '书签管理',
          component: Bookmarks
        }
      ]
    }
  ]
});
router.beforeEach((to, from, next) => {
  // 清除总揽定时器
  if (window.dashboartInterval) {
    window.clearInterval(window.dashboartInterval);
  }
  if (window.driverInterval) {
    window.clearInterval(window.driverInterval);
  }
  if (to.path !== '/order/browse') {
    clearInterval(sessionStorage.getItem('orderTiming'));
  }
  if (to.path === '/login') {
    sessionStorage.removeItem('user');
    if (window.stomp) {
      window.stomp.disconnect();
    }
  }
  let user = JSON.parse(sessionStorage.getItem('user'));
  if (!user && to.path !== '/login') {
    next({path: '/login'});
  } else {
    next();
  }
});
export default router;
